package handlers

import (
	"delivery/cache"
	"delivery/models"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func CarAdd(c *gin.Context) {
	carNo := c.PostForm("car_no")
	carName := c.PostForm("car_name")
	carNum := c.PostForm("car_num")

	if carNo == "" || carName == "" || carNum == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不可为空",
		})
		return
	}

	data := models.Car{
		CarNo:   carNo,
		CarName: carName,
		CarNum:  carNum,
	}

	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return

}

func OrdAdd(c *gin.Context) {
	ordNo := c.PostForm("ord_no")
	ordForm := c.PostForm("ord_form")
	ordTo := c.PostForm("ord_to")
	cid, _ := strconv.Atoi(c.PostForm("cid"))
	if ordNo == "" || ordForm == "" || ordTo == "" || cid == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不可为空",
		})
		return
	}

	data := models.Ord{
		OrdNo:   ordNo,
		OrdForm: ordForm,
		OrdTo:   ordTo,
		Cid:     cid,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return

}

func CarOrdList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "2"))
	offset := (page - 1) * size

	/*
		SELECT
		  ords.id AS OrdId,
		  cars.car_no AS CarNo,
		  cars.car_name AS CarName,
		  cars.car_num AS CarNum,
		  ords.ord_no AS OrdNo,
		  ords.ord_form AS OrdForm,
		  ords.ord_to AS OrdTO
		FROM
		  ords
		  LEFT JOIN cars ON cars.id = ords.cid
		WHERE
		  cars.id = 1
	*/

	var car []models.CarOrdList
	redis_key := "car_list" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("ords").
			Select(
				"ords.id AS OrdId",
				"cars.car_no AS CarNo",
				"cars.car_name AS CarName",
				"cars.car_num AS CarNum",
				"ords.ord_no AS OrdNo",
				"ords.ord_form AS OrdForm",
				"ords.ord_to AS OrdTo",
			).
			Joins("LEFT JOIN cars ON cars.id = ords.cid").
			Offset(offset).
			Limit(size).
			Debug().
			Find(&car)

		jsonData, _ := json.Marshal(car)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &car)
	}

	var count int64
	models.DB.Model(&models.Ord{}).Count(&count)

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": gin.H{
			"count": count,
			"car":   car,
		},
	})
	return
}

func CarOrdDetail(c *gin.Context) {
	id := c.Query("id")

	var car models.CarOrdList
	redis_key := "car_detail" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("ords").
			Select(
				"ords.id AS OrdId",
				"cars.car_no AS CarNo",
				"cars.car_name AS CarName",
				"cars.car_num AS CarNum",
				"ords.ord_no AS OrdNo",
				"ords.ord_form AS OrdForm",
				"ords.ord_to AS OrdTo",
			).
			Joins("LEFT JOIN cars ON cars.id = ords.cid").
			Where("cars.id = ?", id).
			Debug().
			Find(&car)

		jsonData, _ := json.Marshal(car)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &car)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": car,
	})
	return
}
